import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/config/ui_constant.dart';
import 'package:loading_more_list/loading_more_list.dart';

import 'loading_list_state_view.dart';
import 'loading_more_busying_view.dart';

class ListIndicator extends StatelessWidget {
  final IndicatorStatus indicatorStatus;
  final LoadingMoreBase loadingMoreBase;
  final bool isSliver;
  final Widget? loadingChildWidget;
  final Widget? errorChildWidget;
  final Widget? noMoreChildWidget;
  final Widget? emptyChildWidget;

  const ListIndicator(
    this.indicatorStatus,
    this.loadingMoreBase, {
    this.isSliver = false,
    Key? key,
    this.loadingChildWidget,
    this.errorChildWidget,
    this.noMoreChildWidget,
    this.emptyChildWidget,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget widget;
    switch (indicatorStatus) {
      case IndicatorStatus.none:
        widget = Container(height: 0.0);
        break;
      case IndicatorStatus.loadingMoreBusying:
        widget = LoadingMoreBusyingView();
        if (loadingChildWidget != null) {
          widget = loadingChildWidget!;
        }
        break;
      case IndicatorStatus.fullScreenBusying:
        widget = LoadingMoreBusyingView();
        if (loadingChildWidget != null) {
          widget = loadingChildWidget!;
        }
        if (isSliver) {
          widget = SliverFillRemaining(
            child: widget,
          );
        }
        break;
      case IndicatorStatus.error:
        widget = listStateLoadMoreError();

        break;
      case IndicatorStatus.fullScreenError:
        widget = errorWidget();
        if (isSliver) {
          widget = SliverFillRemaining(
            child: widget,
          );
        }
        break;
      case IndicatorStatus.noMoreLoad:
        widget = Center(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 20.w),
            child: Text(
              '数据到底了',
              style: TextStyle(
                color: ColorConf.lightGray,
                fontSize: 12.sp,
              ),
            ),
          ),
        );
        if (noMoreChildWidget != null) {
          widget = noMoreChildWidget!;
        }
        break;
      case IndicatorStatus.empty:
        widget = LoadingListStateView('images/lt_common/no_data.png', '没有查询到数据');
        if (emptyChildWidget != null) {
          widget = emptyChildWidget!;
        }
        if (isSliver) {
          widget = SliverToBoxAdapter(
            child: widget,
          );
        }
        break;
    }
    return widget;
  }

  Widget errorWidget() {
    return GestureDetector(
      onTap: () {
        loadingMoreBase.errorRefresh();
      },
      child: LoadingListStateView(
        'images/lt_common/no_data.png',
        '加载出错了，点击重试',
      ),
    );
  }

  Widget listStateLoadMoreError() {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        loadingMoreBase.errorRefresh();
      },
      child: Container(
        margin: EdgeInsets.symmetric(vertical: 20.w),
        alignment: Alignment.center,
        child: Text(
          '加载出错，请点击重试！',
          style: TextStyle(
            fontSize: 12.sp,
            color: UIConstant.color66,
          ),
        ),
      ),
    );
  }
}
